/**
 * 
 */
package com.tw.udf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.pig.EvalFunc;
import org.apache.pig.data.DataBag;
import org.apache.pig.data.Tuple;
import org.apache.pig.data.TupleFactory;


/**
 * @author gang.qin
 * 2013-8-1
 */
public class UrlPath extends EvalFunc<String> {

	TupleFactory tupleFactory;

	/**
	 * 构造方法，实例化工厂类
	 */
	public UrlPath() {
		tupleFactory = TupleFactory.getInstance();
	}
	
	/**
	 * 返回用户所访问的来源与所有URL列表
	 */
	@Override
	public String exec(Tuple input) throws IOException {
		// TODO Auto-generated method stub
		if (input == null || input.size() == 0)
			return null;
		String urlpath = null;
		List<String> urllist = new ArrayList<String>();
		try {
			DataBag bag = (DataBag) input.get(0);
			if (bag.size() > 0 || bag instanceof DataBag) {
				Iterator<Tuple> it = bag.iterator();
				while (it.hasNext()) {
					Tuple tpl = it.next();
					urllist.add((String) tpl.get(1));
				}
			}
			for (String str : urllist) {
				urlpath += "|"+str;
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return urlpath;
	}

}
